/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import entity.Employee;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import session.stateless.ContactSessionBeanLocal;
import java.util.List;
import java.util.Map;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import org.primefaces.event.SelectEvent;
import session.stateless.EmployeeSessionBeanLocal;

/**
 *
 * @author Wx
 */
@ManagedBean(name = "contactManagerBean")
@RequestScoped
public class ContactManagerBean implements Serializable {

    @ManagedProperty(value = "#{loginManagerBean}")
    private LoginManagerBean loginManagerBean;
    @ManagedProperty(value = "#{messageManagerBean}")
    private MessageManagerBean messageManagerBean;
    @ManagedProperty(value = "#{userSessionManagerBean}")
    private UserSessionManagerBean userSessionManagerBean;
    @EJB
    private ContactSessionBeanLocal contactSessionBeanLocal;
    @EJB
    private EmployeeSessionBeanLocal employeeSessionBeanLocal;
    private String userId;
    private String userPassword;
    private String name;
    private Integer contactOffice;
    private Integer contactMobile;
    private String emailAddress;
    private String department;
    private String designation;
    private Date dob;
    private SelectItem[] departmentOptions;
    private final static String[] departments = {"Finance", "Marketing", "Production and Operation", "Supply Chain", "Sales", "Technology", "Warehouse"};
    private SelectItem[] designationOptions;
    private String[] designations;
    private String loadContactList;
    private String loadContactPage;
    private List<Employee> contacts;
    private Employee employee;
    private Map<String, Boolean> selectedIds = new HashMap<String, Boolean>();
    private List<Employee> selectedContacts;
    private Employee selectedContact;

    /** Creates a new instance of ContactManagerBean */
    public ContactManagerBean() {
        departmentOptions = createDepartmentFilterOptions(departments); //populate filtering option for department
    }

    /**
     * @return the loginManagerBean
     */
    public LoginManagerBean getLoginManagerBean() {
        return loginManagerBean;
    }

    /**
     * @param loginManagerBean the loginManagerBean to set
     */
    public void setLoginManagerBean(LoginManagerBean loginManagerBean) {
        this.loginManagerBean = loginManagerBean;
    }

    /**
     * @return the contactSessionBeanLocal
     */
    public ContactSessionBeanLocal getContactSessionBeanLocal() {
        return contactSessionBeanLocal;
    }

    /**
     * @param contactSessionBeanLocal the contactSessionBeanLocal to set
     */
    public void setContactSessionBeanLocal(ContactSessionBeanLocal contactSessionBeanLocal) {
        this.contactSessionBeanLocal = contactSessionBeanLocal;
    }

    /**
     * @return the userId
     */
    public String getUserId() {
        return userId;
    }

    /**
     * @param userId the userId to set
     */
    public void setUserId(String userId) {
        this.userId = userId;
    }

    /**
     * @return the userPassword
     */
    public String getUserPassword() {
        return userPassword;
    }

    /**
     * @param userPassword the userPassword to set
     */
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the contactOffice
     */
    public Integer getContactOffice() {
        return contactOffice;
    }

    /**
     * @param contactOffice the contactOffice to set
     */
    public void setContactOffice(Integer contactOffice) {
        this.contactOffice = contactOffice;
    }

    /**
     * @return the contactMobile
     */
    public Integer getContactMobile() {
        return contactMobile;
    }

    /**
     * @param contactMobile the contactMobile to set
     */
    public void setContactMobile(Integer contactMobile) {
        this.contactMobile = contactMobile;
    }

    /**
     * @return the emailAddress
     */
    public String getEmailAddress() {
        return emailAddress;
    }

    /**
     * @param emailAddress the emailAddress to set
     */
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    /**
     * @return the department
     */
    public String getDepartment() {
        return department;
    }

    /**
     * @param department the department to set
     */
    public void setDepartment(String department) {
        this.department = department;
    }

    /**
     * @return the designation
     */
    public String getDesignation() {
        return designation;
    }

    /**
     * @param designation the designation to set
     */
    public void setDesignation(String designation) {
        this.designation = designation;
    }

    /**
     * @return the loadContactList
     */
    public String getLoadContactList() {
        loadDesignations(); 
        loadContacts();

        return null;
    }

    public void loadDesignations() { //load designations from database
        List<String> s = employeeSessionBeanLocal.getDesignations();
        int i = 0;
        designations = new String[s.size()];
        
        for (String str : s) {
            designations[i] = str;
            i++;
        }
        
        designationOptions = createDesignationFilterOptions(designations); //populate filtering option for designation
    }

    public void loadContacts() {
        setContacts(contactSessionBeanLocal.getContacts(userSessionManagerBean.getUserId()));
    }

    private SelectItem[] createDepartmentFilterOptions(String[] departments) {
        SelectItem[] options = new SelectItem[departments.length + 1];

        options[0] = new SelectItem("", "Select");
        for (int i = 0; i < departments.length; i++) {
            options[i + 1] = new SelectItem(departments[i], departments[i]);
        }

        return options;
    }

    private SelectItem[] createDesignationFilterOptions(String[] designations) {
        SelectItem[] options = new SelectItem[designations.length + 1];

        options[0] = new SelectItem("", "Select");
        for (int i = 0; i < designations.length; i++) {
            options[i + 1] = new SelectItem(designations[i], designations[i]);
        }

        return options;
    }

    /**
     * @param loadContactList the loadContactList to set
     */
    public void setLoadContactList(String loadContactList) {
        this.loadContactList = loadContactList;
    }

    /**
     * @return the contacts
     */
    public List<Employee> getContacts() {
        return contacts;
    }

    /**
     * @param contacts the contacts to set
     */
    public void setContacts(List<Employee> contacts) {
        this.contacts = contacts;
    }

    public void onRowSelect(SelectEvent event) { //to view profile of contact selected
        try {
            userSessionManagerBean.setSelectedContact(getSelectedContact());
//            setEmployee((Employee) event.getObject());
//            setUserId(getEmployee().getUserId());
//            setName(getEmployee().getName());
//            setDepartment(getEmployee().getDepartment());
//            setDesignation(getEmployee().getDesignation());
//            setContactOffice(getEmployee().getContactOffice());
//            setContactMobile(getEmployee().getContactMobile());
//            setEmailAddress(getEmployee().getEmailAddress());
            
            FacesContext.getCurrentInstance().getExternalContext().redirect("http://" 
                    + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort() 
                    + "/MerlionERP-war/viewProfile.xhtml"); //redirect to viewProfile.xhtml on contact select
        } catch (Exception ex) {
            userSessionManagerBean.setStatus(ex.toString());
        }
    }

    /**
     * @return the employee
     */
    public Employee getEmployee() {
        return employee;
    }

    /**
     * @param employee the employee to set
     */
    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    /**
     * @return the messageManagerBean
     */
    public MessageManagerBean getMessageManagerBean() {
        return messageManagerBean;
    }

    /**
     * @param messageManagerBean the messageManagerBean to set
     */
    public void setMessageManagerBean(MessageManagerBean messageManagerBean) {
        this.messageManagerBean = messageManagerBean;
    }

    public void sendMessage(ActionEvent event) { //to send a message to selected contacts
        try {
            userSessionManagerBean.setReceivers("");
//            selectedContacts = new ArrayList<Employee>();
            
            for (Employee e : contacts) {
                if (selectedIds.get(e.getUserId()).booleanValue()) {
                    if (userSessionManagerBean.getReceivers() != null) {

                        if (userSessionManagerBean.getReceivers().indexOf(e.getUserId()) == -1) {
                            userSessionManagerBean.setReceivers(userSessionManagerBean.getReceivers() + e.getUserId() + ",");

                            selectedIds.remove(e.getUserId());
                        }
                    } else {
                        userSessionManagerBean.setReceivers(e.getUserId() + ",");

                        selectedIds.remove(e.getUserId());
                    }
                }
            }
           
            userSessionManagerBean.setStatus("receivers are " + userSessionManagerBean.getReceivers());
            
            FacesContext.getCurrentInstance().getExternalContext().redirect("http://" 
                    + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort() 
                    + "/MerlionERP-war/sendMessage.xhtml"); //redirect to sendMessage.xhtml
        } catch (Exception ex) {
            userSessionManagerBean.setStatus(ex.toString());
        }
    }
    
    /**
     * @return the selectedIds
     */
    public Map<String, Boolean> getSelectedIds() {
        return selectedIds;
    }

    /**
     * @param selectedIds the selectedIds to set
     */
    public void setSelectedIds(Map<String, Boolean> selectedIds) {
        this.selectedIds = selectedIds;
    }

    /**
     * @return the selectedContacts
     */
    public List<Employee> getSelectedContacts() {
        return selectedContacts;
    }

    /**
     * @param selectedContacts the selectedContacts to set
     */
    public void setSelectedContacts(List<Employee> selectedContacts) {
        this.selectedContacts = selectedContacts;
    }

    /**
     * @return the userSessionManagerBean
     */
    public UserSessionManagerBean getUserSessionManagerBean() {
        return userSessionManagerBean;
    }

    /**
     * @param userSessionManagerBean the userSessionManagerBean to set
     */
    public void setUserSessionManagerBean(UserSessionManagerBean userSessionManagerBean) {
        this.userSessionManagerBean = userSessionManagerBean;
    }

    /**
     * @return the departmentOptions
     */
    public SelectItem[] getDepartmentOptions() {
        return departmentOptions;
    }

    /**
     * @return the designationtOptions
     */
    public SelectItem[] getDesignationOptions() {
        return designationOptions;
    }

    /**
     * @return the selectedContact
     */
    public Employee getSelectedContact() {
        return selectedContact;
    }

    /**
     * @param selectedContact the selectedContact to set
     */
    public void setSelectedContact(Employee selectedContact) {
        this.selectedContact = selectedContact;
    }

    /**
     * @return the loadContactPage
     */
    public String getLoadContactPage() {
        employee = userSessionManagerBean.getSelectedContact();
        
        this.setUserId(employee.getUserId());
        this.setName(employee.getName());
        this.setDepartment(employee.getDepartment());
        this.setDesignation(employee.getDesignation());
        this.setContactMobile(employee.getContactMobile());
        this.setContactOffice(employee.getContactOffice());
        this.setEmailAddress(employee.getEmailAddress());
        this.setDob(employee.getDob());
        
        return null;
    }

    /**
     * @param loadContactPage the loadContactPage to set
     */
    public void setLoadContactPage(String loadContactPage) {
        this.loadContactPage = loadContactPage;
    }

    /**
     * @return the dob
     */
    public Date getDob() {
        return dob;
    }

    /**
     * @param dob the dob to set
     */
    public void setDob(Date dob) {
        this.dob = dob;
    }
}
